#include "gate_module_impl.h"
#include "gate_listener_impl.h"
#include "gate_connector_impl.h"
#include <boost/bind.hpp>
#include <algorithm>

namespace utility
{
namespace network
{
gate_module_impl * gate_module_impl::instance_ = NULL;

gate_module_impl * gate_module_impl::instance()
{
    if (instance_ == NULL)
        instance_ = new gate_module_impl;
    return instance_;
}

void gate_module_impl::destroy_instance()
{
    delete instance_;
    instance_ = NULL;
}

gate_module_impl::gate_module_impl(void)
{
    net_module_ = NULL;
}


gate_module_impl::~gate_module_impl(void)
{
    std::for_each(listeners_.begin(), listeners_.end(), boost::bind(&gate_listener_impl::release, _1));

    std::for_each(connectors_.begin(), connectors_.end(), boost::bind(&gate_connector_impl::release, _1));
}

bool gate_module_impl::initialize(net_module * netmodule)
{
    net_module_ = netmodule;
    return net_module_ != NULL;
}

gate_listener * gate_module_impl::create_listener()
{
    gate_listener_impl * listener = new gate_listener_impl(net_module_);
    listeners_.push_back(listener);
    return listener;
}

gate_connector * gate_module_impl::create_connector()
{
    gate_connector_impl * connector = new gate_connector_impl(net_module_);
    connectors_.push_back(connector);
    return connector;
}

}
}

utility::network::gate_module * get_gate_module()
{
    return utility::network::gate_module_impl::instance();
}

void destroy_gate_module()
{
    utility::network::gate_module_impl::destroy_instance();
}



